package com.sea.finance.service.impl;


import com.codecrane.core.util.DateUtils;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.finance.entity.Inventory;
import com.sea.finance.entity.Payment;
import com.sea.finance.entity.PaymentAgain;
import com.sea.finance.mapper.PaymentAgainMapper;
import com.sea.finance.mapper.PaymentMapper;
import com.sea.finance.service.InventoryService;
import com.sea.finance.service.PaymentAgainService;
import com.sea.finance.service.PaymentService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.text.ParseException;
import java.util.Date;
import java.util.List;


/**
 * @author 陈鸿飞
 */
@Service
public class PaymentAgainServiceImpl implements PaymentAgainService {

    @Autowired
    private PaymentAgainMapper paymentAgainMapper;
    @Autowired
    private InventoryService inventoryService;

    @Override
    public List<PaymentAgain> findByCnd(PaymentAgain paymentAgain) {
        return paymentAgainMapper.findByCnd(paymentAgain);
    }

    @Override
    public int save(PaymentAgain paymentAgain) {
        return paymentAgainMapper.insert(paymentAgain);
    }


    public static void main(String[] args) {

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importPayment(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream, fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<PaymentAgain> list = BatchImportUtils.iteratorForPaymentAgain(sheet);
            if (null == list || list.size() == 0) {
                throw new Exception("请至少新增一条明细！");
            } else {
                Date monthDate = DateUtils.addMonth(new Date(), -1);
                String lastMonth = DateUtils.dateToString(monthDate, "yyyy-MM");
                for (PaymentAgain paymentAgain : list) {
                    save(paymentAgain);
                    if(!lastMonth.equals(paymentAgain.getPaymentDate())){
                        throw new RuntimeException("只能获取上月数据!");
                    }else{
                        Inventory inventory=new Inventory();
                        inventory.setInventoryDate(lastMonth);
                        inventory.setInventorySku(paymentAgain.getPaymentSku());
                        inventory.setInventoryChannelName(paymentAgain.getPaymentChannelName());
                        Inventory inv = inventoryService.findBySkuChannelDate(inventory);
                        if(null==inv){
                            throw new RuntimeException("暂未查询到数据！"+inventory.getInventorySku());
                        }else{
                            //出库单价
                            double v2=(inv.getInventoryBeginMoney() + inv.getInventoryBorrowMoney());
                            Double v3=(double)((inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() + paymentAgain.getPaymentNumber()));
                            System.out.println(v2+"----"+v3);
                            //判断是否正负对冲
                            double v = 0;
                            if(v3!=(0)){
                                v = v2 / v3;
                                //出库金额
                                inv.setInventoryCreditPrice(v);
                                inv.setInventoryCreditMoney(inv.getInventoryCreditNumber()*inv.getInventoryCreditPrice());
                            }else{
                                //出库金额
                                inv.setInventoryCreditPrice(inv.getInventoryBeginPrice());
                                inv.setInventoryCreditMoney(inv.getInventoryCreditNumber()*inv.getInventoryCreditPrice());

                            }
                            //期末数量
                            int endNumber = inv.getInventoryEndNumber() + paymentAgain.getPaymentNumber();
                            inv.setInventoryCreditNumber(inv.getInventoryCreditNumber());

                            inventoryService.updateCreditNumberBySkuChannelDate(inv);
                            inv.setInventoryEndNumber(endNumber);
                            inv.setInventoryEndPrice(inv.getInventoryCreditPrice());
                            inv.setInventoryEndMoney(inv.getInventoryEndNumber()*inv.getInventoryCreditPrice());

                            if(null== inv.getInventoryBeginCustomTax()  ){
                                inv.setInventoryBeginCustomTax(0.00);
                            }
                            if(null==inv.getInventoryBeginCustomFreight()){
                                inv.setInventoryBeginCustomFreight(0.00);
                            }
                            if(inv.getInventoryBorrowNumber().equals(0) && inv.getInventoryBeginNumber().equals(0)){
                                inv.setInventoryEndUnitPrice(0.00);
                                inv.setInventoryEndCustomTax(0.00);
                                inv.setInventoryEndCustomFreight(0.00);
                            }else {
                                if((inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() + paymentAgain.getPaymentNumber())==0){
                                    inv.setInventoryEndUnitPrice(0.00);
                                    inv.setInventoryEndCustomTax(0.00);
                                    inv.setInventoryEndCustomFreight(0.00);
                                    inv.setInventoryEndNumber(0);
                                    inv.setInventoryEndPrice(0.00);
                                    inv.setInventoryEndMoney(0.00);
                                }else{
                                    inv.setInventoryEndUnitPrice((inv.getInventoryBeginUnitPrice() * inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() * inv.getErpProduceUnitPrice()) / (inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() + paymentAgain.getPaymentNumber()));
                                    inv.setInventoryEndCustomTax((inv.getInventoryBeginCustomTax() * inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() * inv.getCreateCustomTax()) / (inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() + paymentAgain.getPaymentNumber()));
                                    inv.setInventoryEndCustomFreight((inv.getInventoryBeginCustomFreight() * inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() * inv.getCreateCustomFreight()) / (inv.getInventoryBeginNumber() + inv.getInventoryBorrowNumber() + paymentAgain.getPaymentNumber()));
                                }
                            }

                            inventoryService.updateEndBySkuChannelDate(inv);
                        }
                    }
                }
            }
        }
    }

    @Override
    public PaymentAgain findBySkuChannel(PaymentAgain paymentAgain) {
        return paymentAgainMapper.findBySkuChannel(paymentAgain);
    }


}
